//
//  InsurancesViewController.m
//  car
//
//  Created by chenxu on 3/31/15.
//  Copyright (c) 2015 chenxu. All rights reserved.
//

#import "InsurancesViewController.h"
#import "InsuranceCell.h"
#import "ThirdViewController.h"
#import "specialTableViewController.h"
#import "PresentingAnimator.h"
#import "DismissingAnimator.h"

@interface InsurancesViewController ()<UIViewControllerTransitioningDelegate>

@property (strong, nonatomic) UILabel *titleLabel, *valueLabel;

@end

@implementation InsurancesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

#pragma mark - table view
#pragma mark Table Data Source Methods
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    [tableView registerClass:[InsuranceCell class] forCellReuseIdentifier:@"insurance"];
   
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 11;
}

#pragma mark Table Delegate Methods
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *CellIdentifier = @"insurance";
    InsuranceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    if (indexPath.row<10) {

        NSString* title = [[NSString alloc]initWithFormat:@"%ld.%@",indexPath.row+1,[_ins getInsuranceName:indexPath.row]];
        [cell setTitle:title];
        
        NSString* row = [[NSString alloc]initWithFormat:@"%ld",(long)indexPath.row+1];
        if ([_ins.selected containsObject:row]) {
            [cell check];
        }
        
        if (indexPath.row == 1) {
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }
    }
    else{
        [cell setTitle: @"11.不计免赔特约险"];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        [cell setMyImage:nil];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [InsuranceCell cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    InsuranceCell* cell =  (InsuranceCell*)[tableView cellForRowAtIndexPath:indexPath];
    
    if([_ins selectAt:indexPath.row+1]){
    
        //显示额外页面
        if (indexPath.row == 1) {
            if (![cell isChecked]) {
                ThirdViewController *thirdViewController = [ThirdViewController new];
                thirdViewController.transitioningDelegate = self;
                thirdViewController.modalPresentationStyle = UIModalPresentationCustom;
                thirdViewController.ins = _ins;
                [self.navigationController presentViewController:thirdViewController
                                                        animated:YES
                                                      completion:NULL];
            }
            [cell changestate];
        }
        else if (indexPath.row == 10){
            specialTableViewController *specialViewController = [specialTableViewController new];
            specialViewController.transitioningDelegate = self;
            specialViewController.modalPresentationStyle = UIModalPresentationCustom;
            specialViewController.ins = _ins;
            [self.navigationController presentViewController:specialViewController
                                                    animated:YES
                                                  completion:NULL];
        }
        else{
            [cell changestate];
        }
        
        
    }
}

#pragma mark - UIViewControllerTransitioningDelegate

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented
                                                                  presentingController:(UIViewController *)presenting
                                                                      sourceController:(UIViewController *)source
{
    return [PresentingAnimator new];
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    return [DismissingAnimator new];
}

@end
